package datagenerator;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Random;
import java.util.Scanner;

public class DataGenerator {

	private ArrayList<String> first;
	private ArrayList<String> last;
	private ArrayList<String> pos;
	private ArrayList<String> field;
	private ArrayList<String> crop;
	private ArrayList<String> treatment;
	private ArrayList<String> purpose;
	private ArrayList<String> dairy;
	private ArrayList<String> meat;
	private ArrayList<String> breeding;
	private ArrayList<String> eggs;
	private ArrayList<String> wool;
	private ArrayList<String> product;
	private ArrayList<String> supplier;
	private int wid = 0;
	private int fid = 0;
	private int hid = 0;
	private int pid = 0;
	private int aid = 0;
	private int did = 0;
	private Random r;

	public DataGenerator() {
		this.r = new Random();
		load();
	}

	public String[] getWorker() {
		String[] ret = new String[5];
		String position = this.pos.get(this.r.nextInt(this.pos.size()));
		int sal = 0;
		if (position.equals("Vet") || position.equals("Inspector")
				|| position.equals("Animal Breeder")) {
			sal = 25;
		}
		ret[0] = Integer.toString(this.wid);
		this.wid++;
		ret[1] = Integer.toString(getSal(sal));
		ret[2] = this.first.get(this.r.nextInt(this.first.size())) + " "
				+ this.last.get(this.r.nextInt(this.last.size()));
		ret[3] = position;
		ret[4] = Integer.toString(this.r.nextInt(10) + 1);
		return ret;
	}
	
	public String[] getManager(){
		String[] ret = new String[5];
		ret[0] = Integer.toString(this.wid);
		ret[1] = Integer.toString(getSal(30));
		ret[2] = this.first.get(this.r.nextInt(this.first.size())) + " "
				+ this.last.get(this.r.nextInt(this.last.size()));
		ret[3] = "Management";
		ret[4] = Integer.toString(this.r.nextInt(10) + 1);
		this.wid++;
		return ret;
	}

	public String[] getPasture() {
		String[] ret = new String[4];
		ret[0] = Integer.toString(this.pid);
		this.pid++;
		ret[1] = getDate();
		ret[2] = getAddress();// needs filling
		ret[3] = this.field.get(this.r.nextInt(this.field.size()));
		return ret;
	}

	public String[] getField() {
		String[] ret = new String[6];
		ret[0] = Integer.toString(this.fid);
		this.fid++;
		ret[1] = this.crop.get(this.r.nextInt(this.crop.size()));
		ret[2] = this.treatment.get(this.r.nextInt(this.treatment.size()));
		ret[3] = getDate();
		ret[4] = Integer.toString(randBetween(100, 1000));
		ret[5] = getAddress();// needs filling
		return ret;
	}

	public String[] getAnimal() {
		String[] ret = new String[6];
		String pur = this.purpose.get(this.r.nextInt(this.purpose.size()));
		String breed = "";
		switch (pur) {
		case "Dairy":
			breed = this.dairy.get(this.r.nextInt(this.dairy.size()));
			break;
		case "Meat":
			breed = this.meat.get(this.r.nextInt(this.meat.size()));
			break;
		case "Breeding":
			breed = this.breeding.get(this.r.nextInt(this.breeding.size()));
			break;
		case "Eggs":
			breed = this.eggs.get(this.r.nextInt(this.eggs.size()));
			break;
		case "Wool":
			breed = this.wool.get(this.r.nextInt(this.wool.size()));
			break;
		}
		ret[0] = Integer.toString(this.aid);
		this.aid++;
		ret[1] = breed;
		ret[2] = pur;
		ret[3] = (r.nextInt(10) < 2 ? "Sick" : "Healthy");
		ret[4] = (r.nextBoolean() ? "M" : "F");
		ret[5] = Integer.toString(r.nextInt(15));

		return ret;
	}

	public String[] getHerd() {
		String[] ret = new String[3];
		ret[0] = Integer.toString(this.hid);
		this.hid++;
		ret[1] = Integer.toString(randBetween(100, 2000));
		ret[2] = Integer.toString(randBetween(100, 2000));
		return ret;
	}

	public String[] getFeed() {
		String[] ret = new String[5];
		ret[0] = Integer.toString(this.did);
		this.did++;
		ret[1] = this.product.get(this.r.nextInt(this.product.size()));
		ret[2] = Integer.toString(randBetween(100, 2000));
		ret[3] = this.supplier.get(this.r.nextInt(this.supplier.size()));
		ret[4] = Integer.toString(randBetween(2, 30));
		return ret;
	}

	private ArrayList<String> getText(String filename) {
		ArrayList<String> ret = new ArrayList<String>();
		try {
			File f = new File(filename);
			Scanner s = new Scanner(new FileReader(f));
			while (s.hasNext()) {
				ret.add(s.nextLine().trim());
			}
			s.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return ret;
	}

	private int getSal(int top) {
		Random r = new Random();
		int ret = r.nextInt(16);
		ret += top + 10;
		ret *= 1000;
		return ret;
	}

	private String getDate() {
		GregorianCalendar gc = new GregorianCalendar();
		int year = randBetween(2000, 2015);
		gc.set(Calendar.YEAR, year);
		int dayOfYear = randBetween(1,
				gc.getActualMaximum(Calendar.DAY_OF_YEAR));
		gc.set(Calendar.DAY_OF_YEAR, dayOfYear);
		int month = gc.get(Calendar.MONTH) + 1;
		String mon = Integer.toString(month);
		if (mon.length() == 1) {
			mon = "0" + mon;
		}
		int day = gc.get(Calendar.DAY_OF_MONTH);
		String d = Integer.toString(day);
		if (d.length() == 1) {
			d = "0" + d;
		}
		return Integer.toString(gc.get(Calendar.YEAR)) + mon + d;
		// code adapted from
		// http://stackoverflow.com/questions/3985392/generate-random-date-of-birth
	}

	private int randBetween(int start, int end) {
		return start + (int) Math.round(Math.random() * (end - start));
		// code from
		// http://stackoverflow.com/questions/3985392/generate-random-date-of-birth
	}

	private String getAddress() {
		String ret = "";
		ret += randBetween(1, 400);
		ret += " " + this.field.get(this.r.nextInt(this.field.size()));
		ret += " Rd.";
		return ret;
	}

	private void load() {
		this.first = getText("firstname.txt");
		this.last = getText("lastname.txt");
		this.pos = getText("position.txt");
		this.field = getText("field.txt");
		this.crop = getText("crop.txt");
		this.treatment = getText("treatment.txt");
		this.purpose = getText("purpose.txt");
		this.dairy = getText("dairy.txt");
		this.eggs = getText("eggs.txt");
		this.meat = getText("meat.txt");
		this.breeding = getText("breeding.txt");
		this.wool = getText("wool.txt");
		this.product = getText("product.txt");
		this.supplier = getText("supplier.txt");

	}
}
